//
//  ViewController.m
//  XidouNetTool
//
//  Created by 喜豆 on 2018/8/14.
//  Copyright © 2018年 seedo. All rights reserved.
//

#import "ViewController.h"


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

-(void)setupView{
    self.headTableView.delegate = self;
    self.headTableView.dataSource = self;
    self.bodyTableView.delegate = self;
    self.bodyTableView.dataSource = self;
}

#pragma mark - lazy init

-(MainModel *)model{
    if (!_model) {
        _model = [[MainModel alloc] init];
    }
    return _model;
}

#pragma mark - Methods


#pragma mark - Actions

- (IBAction)rememberAction:(id)sender {
    
}


- (IBAction)headAction:(id)sender {
    HeaderModel * model = [HeaderModel modelWithField:@"XXX" value:@"JJJJ" remember:YES];
    [self.model.headerModels addObject:model];
    [self.headTableView reloadData];
}

- (IBAction)bodyAction:(id)sender {
    BodyModel * model = [BodyModel modelWithField:@"XXXX" value:@"jjj" remember:NO md5:NO];
    [self.model.bodyModels addObject:model];
    [self.bodyTableView reloadData];
}

#pragma mark - NSTableViewDelegate, NSTableDataSource

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    if (tableView == self.headTableView) {
        return self.model.headerModels.count;
    }
    return self.model.bodyModels.count;
}

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    if (tableView == self.headTableView) {
        HeaderModel * model = self.model.headerModels[row];
        if ([tableColumn.identifier isEqualToString:@"field"]) {
            return model.field;
        }else if([tableColumn.identifier isEqualToString:@"value"]){
            return model.value;
        }else{
            return @(model.remember);
        }
    }else{
        BodyModel * model = self.model.bodyModels[row];
        NSLog(@"%@", tableColumn.identifier);
        if ([tableColumn.identifier isEqualToString:@"field"]){
            return model.field;
        }else if ([tableColumn.identifier isEqualToString:@"value"]){
            return model.value;
        }else {
            NSLog(@"%@" ,tableColumn.dataCell);
            NSTextFieldCell * cell = tableColumn.dataCell;
            return @(model.remember);
        }
    }
}


- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    if (tableView == self.headTableView) {
        NSLog(@"head table");
    }else{
        NSLog(@"bottom table");
    }
    
    NSLog(@"%@",tableColumn.identifier);
    NSLog(@"%ld",row);
}

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
    return nil;
}
@end
